import axios from "axios"

const service = axios.create()

// 请求拦截器
service.interceptors.request.use(function (config) {
  let token = localStorage.getItem("bku_token")
  // 在发送请求之前传递token
  if (token) {
    config.headers.Authorization = token
  }
  return config;
}, function (error) {
  // 请求错误
  return Promise.reject(error);
});

// 响应拦截器
service.interceptors.response.use(function (response) {
  return response;
}, function (error) {
  switch (error.response.status) {
    case 404:
      console.log('操作失败，错误404')
      break;
    case 401:
      console.log('操作失败，错误401')
      break;
    case 500:
      console.log('操作失败，错误500')
      break;
    default:
      console.log('操作失败，错误未知')
      break;
  }
  return Promise.reject(error);
});

export default service